package com.hsav.kcxb.adapter;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.hsav.kcxb.fragment.SongListFragment;

/**
 * Created by zhao on 2016/12/16.
 */

public abstract class LoadMoreListener extends RecyclerView.OnScrollListener implements com.hsav.kcxb.inter.OnLoadMoreListener {

    private GridLayoutManager mGridLayoutManager;
    private int mTotalCount;
    //存储上一个总数
    private int mVisibleCount;
    private int mFirstVisiblePos;
    private int mLastVisiblePos;
    private boolean mLoading = false;

    public static enum LayoutManagerType{
        LINEAR_LAYOUT,GRID_LAYOUT,STAGGERED_LAYOUT;
    }

    protected LayoutManagerType mLayoutManagerType;

    public boolean isLoadingMore(){
        return this.mLoading;
    }

    public void setLoading(boolean isLoading){
        this.mLoading = isLoading;
    }

    public LoadMoreListener(GridLayoutManager gridLayoutManager) {
        this.mGridLayoutManager = gridLayoutManager;
    }


    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        if(dy!=0){
            mVisibleCount = mGridLayoutManager.getChildCount();
            mTotalCount = mGridLayoutManager.getItemCount();
            if(mVisibleCount>0 && mLastVisiblePos +1 >= mTotalCount){
                if(!isLoadingMore()){
                    setLoading(true);
                    onStart();
                    onLoadMore();
                }
            }
        }
    }

    public abstract void onLoadMore(int mCurrentPage);
}
